package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskDetailsViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author brianxia
 * @version 1.0
 * @date 2022-12-03 15:24
 */
@Component
@Topic(TopicConfig.TASK_AUTO_SUPPLY_TOPIC)
@Slf4j
public class AutoSupplyHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;
    @Autowired
    private VMService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        if(!StringUtils.isEmpty(jsonMsg)){
            SupplyContract contract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);
            if(StringUtils.isEmpty(contract.getInnerCode()) ||
                    CollectionUtils.isEmpty(contract.getSupplyData())){
                log.error("自动补货消息内容为空" + jsonMsg);
                return;
            }

            TaskViewModel taskViewModel = new TaskViewModel();
            //设置创建类型为自动0
            taskViewModel.setCreateType(VMSystem.TASK_CREATE_TYPE_AUTO);
            //设置售货机编号
            taskViewModel.setInnerCode(contract.getInnerCode());
            //获取售货机信息
            VmVO vmInfo = vmService.getVMInfo(contract.getInnerCode());
            if(vmInfo == null){
                log.error("售货机不存在" + contract.getInnerCode());
                return;
            }
            //设置任务执行人
            Integer leastTaskUser = taskService.getLeastTaskUser(vmInfo.getRegionId(), false);
            if(leastTaskUser == null){
                log.error("最少工单运维人员不存在，区域：" + vmInfo.getRegionId());
                return;
            }
            taskViewModel.setUserId(leastTaskUser);
            //设置默认分配人
            taskViewModel.setAssignorId(0);
            //设置工单类型为维修
            taskViewModel.setProductType(VMSystem.TASK_TYPE_SUPPLY);

            List<TaskDetailsViewModel> collect = contract.getSupplyData().stream().map(x -> {
                TaskDetailsViewModel taskDetailsViewModel = new TaskDetailsViewModel();
                taskDetailsViewModel.setChannelCode(x.getChannelId());
                taskDetailsViewModel.setExpectCapacity(x.getCapacity());
                taskDetailsViewModel.setSkuId(x.getSkuId());
                taskDetailsViewModel.setSkuName(x.getSkuName());
                taskDetailsViewModel.setSkuImage(x.getSkuImage());
                return taskDetailsViewModel;
            }).collect(Collectors.toList());
            taskViewModel.setDetails(collect);
            //创建自动工单
            taskService.create(taskViewModel);
        }
    }

    public static void main(String[] args) {
        HashSet<Object> objects = new HashSet<>();
        objects.add(1);
        objects.add(1);
    }


}
